﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core;
using MvcEngine.Core.Registry;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Gallery.Models;
using MvcEngine.Gallery.ModelBinders;
using MvcEngine.Gallery.StarterSite.Controllers;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Gallery.Services
{
    public class GalleryCategoryControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_GalleryCategoryList",
                "BackOffice/Gallery/{galleryName}/Categories",
                new { controller = "GalleryCategory", action = "List" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryCategoryPaging",
                "BackOffice/Gallery/{galleryName}/Categories/Page{pageNumber}",
                new { controller = "GalleryCategory", action = "List", pageNumber = "0" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryNewCategory",
                "BackOffice/Gallery/{galleryName}/Category/New",
                new { controller = "GalleryCategory", action = "Edit", id = "" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryEditCategory",
                "BackOffice/Gallery/{galleryName}/Category/Edit/{id}",
                new { controller = "GalleryCategory", action = "Edit" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryDeleteCategory",
                "BackOffice/Gallery/{galleryName}/Category/Delete/{id}",
                new { controller = "GalleryCategory", action = "Delete" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );




        }

        public override void RegisterActionFilters(IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryCategoryController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Gallery.BackOffice.Filters.GalleryActionFilter));

            ControllerCriteria starterSiteCommonCriteria = new ControllerCriteria();
            starterSiteCommonCriteria.AddController(typeof(MvcEngine.Gallery.StarterSite.Controllers.GalleryCategoryController));
            registry.Add(new[] { starterSiteCommonCriteria }, typeof(MvcEngine.Gallery.StarterSite.Filters.GalleryActionFilter));

            ////--------------------------------------------------------------------------------------
            ////------------------------------End of common filters-----------------------------------
            ////--------------------------------------------------------------------------------------


            ControllerCriteria moduleCriteria = new ControllerCriteria();
            moduleCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryCategoryController));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());


            ////--------------------------------------------------------------------------------------
            ////---------------------------------Permissions------------------------------------------
            ////--------------------------------------------------------------------------------------
            //ControllerCriteria viewCriteria = new ControllerCriteria();
            //viewCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogCategoryController));

            //ControllerCriteria editCriteria = new ControllerCriteria();
            //editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCategoryController));
            ////--------------------------------------------------------------------------------------
            ////---------------------------------End of permissions----------------------------------
            ////--------------------------------------------------------------------------------------

            //registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            //registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(GalleryCategory)] = new GalleryCategoryModelBinder();
        }

        public override Type GetControllerType()
        {
            return typeof(GalleryCategoryController);
        }

        public override string GetDefaultAction()
        {
            return "Index";
        }
    }
}
